﻿namespace WellFormattedZhouyiGenerator.Writers;
internal abstract class MarkdownWriter : IDocumentWriter
{
    protected StreamWriter InnerStream { get; }

    protected MarkdownWriter(FileInfo file)
    {
        file.Directory?.Create();
        this.InnerStream = new StreamWriter(file.FullName);
    }

    public virtual void PreWrite()
    {

    }

    public virtual void WriteLine(string? s)
    {
        var markdownNewLine = $"{this.InnerStream.NewLine}{this.InnerStream.NewLine}";
        this.InnerStream.WriteLine(s?.ReplaceLineEndings(markdownNewLine));
        this.InnerStream.WriteLine();
    }

    public virtual void WriteTitle(string? s, int level = 1)
    {
        for (int i = 0; i < level; i++)
            this.InnerStream.Write('#');
        this.InnerStream.Write(' ');
        var markdownNewLine = $"{this.InnerStream.NewLine}{this.InnerStream.NewLine}";
        this.InnerStream.WriteLine(s?.ReplaceLineEndings(markdownNewLine));
        this.InnerStream.WriteLine();
    }

    public virtual void PostWrite()
    {

    }

    public virtual void Dispose()
    {
        this.InnerStream.Dispose();
    }
}
